package com.xiaoyao.myframeapp.mvp.base;


import com.xiaoyao.myframeapp.mvp.IModel;
import com.xiaoyao.myframeapp.mvp.IPresenter;
import com.xiaoyao.myframeapp.mvp.IView;

import java.lang.ref.WeakReference;
import java.util.HashMap;

/**
 * @author xiaoyao
 * @mail xiao80206040@qq.com
 * @explain BasePresenter
 * @Created by Administrator on 2017/2/27 0027.
 */

public abstract class BasePresenter<V extends IView> implements IPresenter {
    private WeakReference actReference;

    public abstract HashMap<String, IModel> getiModelMap();

    @Override
    public void attachView(IView iView) {
        actReference = new WeakReference(iView);
    }

    @Override
    public void detachView() {
//        //取消注册，以避免内存泄露
        if (actReference != null) {
            actReference.clear();
            actReference = null;
        }
    }

    @Override
    public V getIView() {
        if (actReference == null) {
            return null;
        }
        return (V) actReference.get();
    }

    /**
     * @param models
     * @return 添加多个model, 如有需要
     */
    public abstract HashMap<String, IModel> loadModelMap(IModel... models);

}
